package com.xdja.netty.util;

import io.netty.channel.DefaultFileRegion;
import io.netty.channel.FileRegion;

import java.io.IOException;
import java.io.InputStream;

/**
 * description
 *
 * @author zk
 * @since 2018/7/23
 */
public class ResourceUtil {

    private static ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    /**
     * 获取资源内容
     *
     * @param path path
     * @return content of html
     */
    public static String getResourceContent(String path) {
        return getInputStreamString(classLoader.getResourceAsStream(path));
    }

    /**
     * 获取inputstream内容
     *
     * @param inputStream inputStream
     * @return content
     */
    private static String getInputStreamString(InputStream inputStream) {
        try {
            int length = inputStream.available();
            byte[] bytes = new byte[length];
            inputStream.read(bytes);
            return new String(bytes,"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
}
